package com.A21MobileSales.JsonServices;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.StrictMode;

import com.A21.Object.A21OPPriceBook;
import com.A21.Object.A21OPPriceBookDetail;
import com.A21.Object.A21OPPriceBookDetailHolder;
import com.A21.Object.A21OPPriceBookHolder;
import com.A21.Object.FrequentPurchaseList;
import com.A21.Object.FrequentPurchaseListHolder;
import com.A21.Object.MSTCompany;
import com.A21.Object.MSTCompanyHolder;
import com.A21.Object.MSTCustomer;
import com.A21.Object.MSTCustomerHolder;
import com.A21.Object.MSTCustomerLocation;
import com.A21.Object.MSTCustomerLocationHolder;
import com.A21.Object.MSTGroupPriceTable;
import com.A21.Object.MSTGroupPriceTableHolder;
import com.A21.Object.MSTMultiTierDiscountOP;
import com.A21.Object.MSTMultiTierDiscountOPHolder;
import com.A21.Object.MSTPriceBracket;
import com.A21.Object.MSTPriceBracketHolder;
import com.A21.Object.MSTPriceTable;
import com.A21.Object.MSTPriceTableHolder;
import com.A21.Object.MSTProduct;
import com.A21.Object.MSTProductHolder;
import com.A21.Object.MSTSettings;
import com.A21.Object.MSTSettingsHolder;
import com.A21.Object.MSTUser;
import com.A21.Object.MSTUserHolder;
import com.A21.Object.Menu;
import com.A21.Object.MenuHolder;
import com.A21.Object.SMGSTType;
import com.A21.Object.SMGSTTypeHolder;
import com.A21.Object.SMUnitConversion;
import com.A21.Object.SMUnitConversionHolder;
import com.A21.Object.SMUnitConversionProd;
import com.A21.Object.SMUnitConversionProdHolder;
import com.A21.Object.SMUnitofMeasure;
import com.A21.Object.SMUnitofMeasureHolder;
import com.A21.Object.SUProductBrand;
import com.A21.Object.SUProductBrandHolder;
import com.A21.Object.SUProductGroup;
import com.A21.Object.SUProductGroupHolder;
import com.A21.Object.SUProductMade;
import com.A21.Object.SUProductMadeHolder;
import com.A21.Object.SUProductModel;
import com.A21.Object.SUProductModelHolder;
import com.A21MobileSales.DataBase.DatabaseHelper;
import com.A21MobileSales.DataBase.MyClass;
import com.google.gson.Gson;

public class Download {

	DatabaseHelper objDataHelper;
	Activity _Activity;
	String _UserName, _PassWord;

	public Download(Activity _activity, String _userName, String _password) {
		_Activity = _activity;
		_UserName = _userName;
		_PassWord = _password;
	}

	private static InputStream OpenHttpConnection(String urlString)
			throws IOException {
		InputStream in = null;
		int response = -1; 
		URL url = new URL(urlString);
		URLConnection conn = url.openConnection();

		if (!(conn instanceof HttpURLConnection))
			throw new IOException("Not an HTTP connection");

		try {
			HttpURLConnection httpConn = (HttpURLConnection) conn;
			httpConn.setAllowUserInteraction(false);
			httpConn.setInstanceFollowRedirects(true);
			httpConn.setRequestMethod("GET");
			httpConn.connect();

			response = httpConn.getResponseCode();
			if (response == HttpURLConnection.HTTP_OK) {
				in = httpConn.getInputStream();
			}
		} catch (Exception ex) {
			throw new IOException("Error connecting");
		}
		return in;
	}

	 
	public void  GetImage(String name) throws Exception {
		String URL = MyClass.getSettingstrDownlaodurl().replace("datadownload.aspx", name +".png");
		Bitmap bitmap = null;
		InputStream in = null;
		try {
			in = OpenHttpConnection(URL);
			bitmap = BitmapFactory.decodeStream(in);
			in.close(); 
			byte[] imgArray = null;
			if (bitmap != null) {
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
				imgArray = stream.toByteArray();
				objDataHelper = new DatabaseHelper(_Activity); 
				objDataHelper.UpdateSystemImage(imgArray,name);
			} 
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} 
	}
	
	public void  GetMenu() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetMenu&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"Menufields\":" + JsonString + "}";
			MenuHolder data = new Gson().fromJson(JsonString,
					MenuHolder.class);
			ArrayList<Menu> rows = new ArrayList<Menu>(
					Arrays.asList(data.Menufields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertMenu(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}
	
	
	public void GetAllUsers() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetAllUsers&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTUserfields\":" + JsonString + "}";
			MSTUserHolder data = new Gson().fromJson(JsonString,
					MSTUserHolder.class);
			ArrayList<MSTUser> Users = new ArrayList<MSTUser>(
					Arrays.asList(data.MSTUserfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertMSTUers(Users);

		} catch (Exception ex) {
			throw ex;
		}
	}
	
	public void GetAllBrand() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetAllBrand&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SUProductBrandfields\":" + JsonString + "}";
			SUProductBrandHolder data = new Gson().fromJson(JsonString,
					SUProductBrandHolder.class);
			ArrayList<SUProductBrand> items = new ArrayList<SUProductBrand>(
					Arrays.asList(data.SUProductBrandfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertBrand(items);

		} catch (Exception ex) {
			throw ex;
		}
	}
	
	
	public void GetAllMade() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetAllMade&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SUProductMadefields\":" + JsonString + "}";
			SUProductMadeHolder data = new Gson().fromJson(JsonString,
					SUProductMadeHolder.class);
			ArrayList<SUProductMade> items = new ArrayList<SUProductMade>(
					Arrays.asList(data.SUProductMadefields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertMade(items);

		} catch (Exception ex) {
			throw ex;
		}
	}
	
	public void GetAllModel() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetAllModel&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SUProductModelfields\":" + JsonString + "}";
			SUProductModelHolder data = new Gson().fromJson(JsonString,
					SUProductModelHolder.class);
			ArrayList<SUProductModel> items = new ArrayList<SUProductModel>(
					Arrays.asList(data.SUProductModelfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertModel(items);

		} catch (Exception ex) {
			throw ex;
		}
	}
	
	public void GetMSTMultiTierDiscountOP() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetMSTMultiTierDiscountOP&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTMultiTierDiscountOPfields\":" + JsonString + "}";
			MSTMultiTierDiscountOPHolder data = new Gson().fromJson(JsonString,
					MSTMultiTierDiscountOPHolder.class);
			ArrayList<MSTMultiTierDiscountOP> items = new ArrayList<MSTMultiTierDiscountOP>(
					Arrays.asList(data.MSTMultiTierDiscountOPfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertMSTMultiTierDiscountOP(items);

		} catch (Exception ex) {
			throw ex;
		}
	}

	

	public String GetInvoiceDetails(String trntype,String trnno) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetAnalysisInvoice&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord) 
					+ "&trntype=" + trntype	+ "&trnno=" + trnno;

			URL url = new URL(Url);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String  htmlCode = new String();
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				htmlCode = htmlCode + inputLine; 
			} 
			return htmlCode;
		} catch (Exception ex) {
			throw ex;
		}
	}
	
	
	public String GetProductInvoiceHistory(String product, String fdate,String tdate) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetAnalysisProduct&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord) 
					+ "&product=" + product+ "&fdate=" + fdate+ "&tdate=" + tdate;

			URL url = new URL(Url);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String  htmlCode = new String();
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				htmlCode = htmlCode + inputLine; 
			} 
			return htmlCode;
		} catch (Exception ex) {
			throw ex;
		}
	}
	
	
	public String GetCustomerInvoiceHistory(String Code, String fdate,String tdate) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetAnalysisCustomer&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord) 
					+ "&customercode=" +URLEncoder.encode( Code) +"&fdate=" + fdate+ "&tdate=" + tdate;

			URL url = new URL(Url);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String  htmlCode = new String();
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				htmlCode = htmlCode + inputLine; 
			} 
			return htmlCode;
		} catch (Exception ex) {
			throw ex;
		}
	}
	
	public String GetSOA(String SOAtype, String Code) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetStatementOfAccount&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord) + "&SOAtype=" + SOAtype
					+ "&customercode=" + URLEncoder.encode(Code);

			URL url = new URL(Url);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String  htmlCode = new String();
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				htmlCode = htmlCode + inputLine; 
			} 
			return htmlCode;
		} catch (Exception ex) {
			throw ex;
		}
	}
	
	public String GetWHQ(String Code) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetWarehouseStock&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord)  
					+ "&product=" + Code;

			URL url = new URL(Url);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String  htmlCode = new String();
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				htmlCode = htmlCode + inputLine; 
			} 
			return htmlCode;
		} catch (Exception ex) {
			throw ex;
		}
	}
	
	public String GetOnSOQty(String Code) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetProductOnSO&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord)  
					+ "&product=" + Code;

			URL url = new URL(Url);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String  htmlCode = new String();
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				htmlCode = htmlCode + inputLine; 
			} 
			return htmlCode;
		} catch (Exception ex) {
			throw ex;
		}
	}
	
	public String GetOnPO(String Code) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetProductOnPO&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord)  
					+ "&product=" + Code;

			URL url = new URL(Url);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String  htmlCode = new String();
			String inputLine;

			while ((inputLine = in.readLine()) != null) {
				htmlCode = htmlCode + inputLine; 
			} 
			return htmlCode;
		} catch (Exception ex) {
			throw ex;
		}
	}
	public void GetCompany() throws Exception {
		try {

			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetCompanyInfos&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTCompanyfields\":" + JsonString + "}";
			MSTCompanyHolder data = new Gson().fromJson(JsonString,
					MSTCompanyHolder.class);
			ArrayList<MSTCompany> rows = new ArrayList<MSTCompany>(
					Arrays.asList(data.MSTCompanyfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertCompany(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllCustomers() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetCustomers&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTCustomerfields\":" + JsonString + "}";
			MSTCustomerHolder data = new Gson().fromJson(JsonString,
					MSTCustomerHolder.class);
			ArrayList<MSTCustomer> rows = new ArrayList<MSTCustomer>(
					Arrays.asList(data.MSTCustomerfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertCustomers(rows);
			objDataHelper.DeleteTable("FrequentPurchaseList");
			for (MSTCustomer item : rows) {
				GetFrequentList(item.Code);
			}
		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllCustomersLocations() throws Exception {
		try {

			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetCustomerLocations&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTCustomerLocationfields\":" + JsonString + "}";
			MSTCustomerLocationHolder data = new Gson().fromJson(JsonString,
					MSTCustomerLocationHolder.class);
			ArrayList<MSTCustomerLocation> rows = new ArrayList<MSTCustomerLocation>(
					Arrays.asList(data.MSTCustomerLocationfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertCustomersLocations(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllGSTTypes() throws Exception {
		try {

			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetGSTTypes&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SMGSTTypefields\":" + JsonString + "}";
			SMGSTTypeHolder data = new Gson().fromJson(JsonString,
					SMGSTTypeHolder.class);
			ArrayList<SMGSTType> rows = new ArrayList<SMGSTType>(
					Arrays.asList(data.SMGSTTypefields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertGST(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllGroupPriceTables() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetGroupPriceTables&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTGroupPriceTablefields\":" + JsonString + "}";
			MSTGroupPriceTableHolder data = new Gson().fromJson(JsonString,
					MSTGroupPriceTableHolder.class);
			ArrayList<MSTGroupPriceTable> rows = new ArrayList<MSTGroupPriceTable>(
					Arrays.asList(data.MSTGroupPriceTablefields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertGroupPriceTables(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllPriceTables() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetPriceTables&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTPriceTablefields\":" + JsonString + "}";
			MSTPriceTableHolder data = new Gson().fromJson(JsonString,
					MSTPriceTableHolder.class);
			ArrayList<MSTPriceTable> rows = new ArrayList<MSTPriceTable>(
					Arrays.asList(data.MSTPriceTablefields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertPriceTables(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllProductGroups() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetProductGroups&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SUProductGroupfields\":" + JsonString + "}";
			SUProductGroupHolder data = new Gson().fromJson(JsonString,
					SUProductGroupHolder.class);
			ArrayList<SUProductGroup> rows = new ArrayList<SUProductGroup>(
					Arrays.asList(data.SUProductGroupfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertProductGroups(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllProducts() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetProducts&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTProductfields\":" + JsonString + "}";
			MSTProductHolder data = new Gson().fromJson(JsonString,
					MSTProductHolder.class);
			ArrayList<MSTProduct> rows = new ArrayList<MSTProduct>(
					Arrays.asList(data.MSTProductfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertProducts(rows); 
		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllUnitConversionByProducts() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetUnitConversionByProducts&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SMUnitConversionProdfields\":" + JsonString + "}";
			SMUnitConversionProdHolder data = new Gson().fromJson(JsonString,
					SMUnitConversionProdHolder.class);
			ArrayList<SMUnitConversionProd> rows = new ArrayList<SMUnitConversionProd>(
					Arrays.asList(data.SMUnitConversionProdfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertUnitConversionByProducts(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetAllUnitofMeasures() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetUnitofMeasures&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SMUnitofMeasurefields\":" + JsonString + "}";
			SMUnitofMeasureHolder data = new Gson().fromJson(JsonString,
					SMUnitofMeasureHolder.class);
			ArrayList<SMUnitofMeasure> rows = new ArrayList<SMUnitofMeasure>(
					Arrays.asList(data.SMUnitofMeasurefields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertUnitofMeasure(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetSystemSettings() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetSystemSettings&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTSettingsfields\":" + JsonString + "}";
			MSTSettingsHolder data = new Gson().fromJson(JsonString,
					MSTSettingsHolder.class);
			ArrayList<MSTSettings> rows = new ArrayList<MSTSettings>(
					Arrays.asList(data.MSTSettingsfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertSystemSettings(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetPriceBooks() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetPriceBooks&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"A21OPPriceBookfields\":" + JsonString + "}";
			A21OPPriceBookHolder data = new Gson().fromJson(JsonString,
					A21OPPriceBookHolder.class);
			ArrayList<A21OPPriceBook> rows = new ArrayList<A21OPPriceBook>(
					Arrays.asList(data.A21OPPriceBookfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertA21OPPriceBooks(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetPriceBookDetails() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetPriceBookDetails&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"A21OPPriceBookDetailfields\":" + JsonString + "}";
			A21OPPriceBookDetailHolder data = new Gson().fromJson(JsonString,
					A21OPPriceBookDetailHolder.class);
			ArrayList<A21OPPriceBookDetail> rows = new ArrayList<A21OPPriceBookDetail>(
					Arrays.asList(data.A21OPPriceBookDetailfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertA21OPPriceBookDetails(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetPriceBrackets() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetPriceBrackets&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"MSTPriceBracketfields\":" + JsonString + "}";
			MSTPriceBracketHolder data = new Gson().fromJson(JsonString,
					MSTPriceBracketHolder.class);
			ArrayList<MSTPriceBracket> rows = new ArrayList<MSTPriceBracket>(
					Arrays.asList(data.MSTPriceBracketfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertMSTPriceBrackets(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetUnitConversions() throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetUnitConversions&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&password="
					+ URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"SMUnitConversionfields\":" + JsonString + "}";
			SMUnitConversionHolder data = new Gson().fromJson(JsonString,
					SMUnitConversionHolder.class);
			ArrayList<SMUnitConversion> rows = new ArrayList<SMUnitConversion>(
					Arrays.asList(data.SMUnitConversionfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertSMUnitConversions(rows);

		} catch (Exception ex) {
			throw ex;
		}
	}

	public void GetFrequentList(String _Code) throws Exception {
		try {
			NetWorkIOError();
			String Url = MyClass.getSettingstrDownlaodurl().equals("0") ? ""
					: MyClass.getSettingstrDownlaodurl()
							+ "?function=GetFrequentPurchaseList&Top=100&username=";
			Url = Url + URLEncoder.encode(_UserName) + "&customercode=" + URLEncoder.encode(_Code)
					+ "&password=" + URLEncoder.encode(_PassWord);
			JSONParser jParser = new JSONParser();
			String JsonString = jParser.getJSONStringFromUrl(Url);
			JsonString = "{\"FrequentPurchaseListfields\":" + JsonString + "}";
			FrequentPurchaseListHolder data = new Gson().fromJson(JsonString,
					FrequentPurchaseListHolder.class);
			ArrayList<FrequentPurchaseList> rows = new ArrayList<FrequentPurchaseList>(
					Arrays.asList(data.FrequentPurchaseListfields));
			objDataHelper = new DatabaseHelper(_Activity);
			objDataHelper.InsertFrequentLists(rows, _Code);

		} catch (Exception ex) {
			throw ex;
		}
	}

	@SuppressLint("NewApi")
	public void NetWorkIOError() {
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
	}

}
